package com.lzy.cloud.base.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.base.dto.BaseRuleDto;
import com.lzy.cloud.base.request.BaseRuleRequest;
import com.lzy.cloud.base.response.BaseRuleResponse;
import com.lzy.cloud.base.service.RuleService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 规则
 */
@RestController
@RequestMapping(value = "/rule")
@Slf4j
public class RuleController {

    @Autowired
    private RuleService ruleService;

    /**
     * 查询规则列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Result queryList(@RequestBody BaseRuleRequest request) {
        Ensure.that(request).isNotNull("00000002");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<BaseRuleDto> pageInfo = ruleService.queryList(pager,DozerUtils.map(request,BaseRuleDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), BaseRuleResponse.class));
        } else {
            return new Result();
        }
    }

    /**
     * 新增规则
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestHeader("token") String token, BaseRuleRequest request) {
        Ensure.that(request).isNotNull("00000002");
        //        UserToken userToken = isLogin(token);
//        request.setUpdateUser(userToken.getId());
        return new Result(ruleService.insert(DozerUtils.map(request, BaseRuleDto.class)));
    }

    /**
     * 更新规则
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestHeader("token") String token, BaseRuleRequest request) {
        Ensure.that(request).isNotNull("00000002");
//        UserToken userToken = isLogin(token);
//        request.setUpdateUser(userToken.getId());
        Ensure.that(ruleService.update(DozerUtils.map(request,BaseRuleDto.class))).isTrue("00040002");
        return new Result();
    }

    /**
     * 规则详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public Result detail(@PathVariable("id") Long id) {
        BaseRuleDto ruleDto = ruleService.find(id);
        return new Result(DozerUtils.map(ruleDto, BaseRuleResponse.class));
    }

    /**
     * 根据id删除规则，假删
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result delete(@PathVariable("id") Long id) {
        Ensure.that(ruleService.deleteById(id)).isTrue("00050005");
        return new Result();
    }


}
